package com.qiangesoft.wechat.core.service.impl;

import com.qiangesoft.wechat.core.service.IWxKefuMessageService;
import com.qiangesoft.wechat.core.service.IWxMaterialService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.common.bean.result.WxMediaUploadResult;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.WxMpMassOpenIdsMessage;
import me.chanjar.weixin.mp.bean.kefu.WxMpKefuMessage;
import me.chanjar.weixin.mp.bean.result.WxMpMassSendResult;
import org.springframework.stereotype.Component;

import java.io.InputStream;
import java.util.List;

/**
 * 客服信息服务
 *
 * @author qiangesoft
 * @date 2023-09-07
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class WxKefuMessageServiceImpl implements IWxKefuMessageService {

    private final WxMpService wxMpService;

    private final IWxMaterialService wxMaterialService;

    @Override
    public String sendTextMsg(String openId, String content) throws WxErrorException {
        WxMpKefuMessage message = new WxMpKefuMessage();
        message.setToUser(openId);
        message.setMsgType(WxConsts.KefuMsgType.TEXT);
        message.setContent(content);
        return wxMpService.getKefuService().sendKefuMessageWithResponse(message);
    }

    @Override
    public String sendImageMsg(String openId, InputStream inputStream) throws WxErrorException {
        String mediaId = wxMaterialService.upload(WxConsts.MediaFileType.IMAGE, "png", inputStream);

        WxMpKefuMessage massMessage = new WxMpKefuMessage();
        massMessage.setToUser(openId);
        massMessage.setMsgType(WxConsts.KefuMsgType.IMAGE);
        massMessage.setMediaId(mediaId);
        return wxMpService.getKefuService().sendKefuMessageWithResponse(massMessage);
    }

    @Override
    public String sendImageMsg(String openId, String mediaId) throws WxErrorException {
        WxMpKefuMessage massMessage = new WxMpKefuMessage();
        massMessage.setToUser(openId);
        massMessage.setMsgType(WxConsts.KefuMsgType.IMAGE);
        massMessage.setMediaId(mediaId);
        return wxMpService.getKefuService().sendKefuMessageWithResponse(massMessage);
    }

    @Override
    public String sendNewsMsg(String openId, List<WxMpKefuMessage.WxArticle> articleList) throws WxErrorException {
        WxMpKefuMessage message = new WxMpKefuMessage();
        message.setToUser(openId);
        message.setMsgType(WxConsts.KefuMsgType.NEWS);
        message.setArticles(articleList);
        return wxMpService.getKefuService().sendKefuMessageWithResponse(message);
    }

    @Override
    public WxMpMassSendResult massSendTextMsg(List<String> openIdList, String content) throws WxErrorException {
        WxMpMassOpenIdsMessage massMessage = new WxMpMassOpenIdsMessage();
        massMessage.setMsgType(WxConsts.MassMsgType.TEXT);
        massMessage.setContent(content);
        massMessage.setToUsers(openIdList);
        return wxMpService.getMassMessageService().massOpenIdsMessageSend(massMessage);
    }

    @Override
    public WxMpMassSendResult massSendImageMsg(List<String> openIdList, InputStream inputStream) throws WxErrorException {
        WxMediaUploadResult uploadResult = wxMpService.getMaterialService().mediaUpload(WxConsts.MediaFileType.IMAGE, "png", inputStream);

        WxMpMassOpenIdsMessage massMessage = new WxMpMassOpenIdsMessage();
        massMessage.setMsgType(WxConsts.MassMsgType.IMAGE);
        massMessage.setMediaId(uploadResult.getMediaId());
        massMessage.setToUsers(openIdList);
        return wxMpService.getMassMessageService().massOpenIdsMessageSend(massMessage);
    }

}
